import { Entity, Column, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, JoinColumn, OneToOne, OneToMany, CreateDateColumn } from 'typeorm';
import { ApiProperty} from '@nestjs/swagger';
import {
  IsOptional,
  IsDefined,
  IsString,
  MaxLength,
  MinLength,
  IsNotEmpty,
  IsEmail,
  IsPhoneNumber,
  IsMobilePhone,
  IsDate,
  ValidateNested, IsDateString
} from "class-validator";
import { CrudValidationGroups } from '@nestjsx/crud';
import { UserLevel } from './user_level.entities';
import { Comment } from './comment.entity';
import { Address } from './address.entities';
import { Order } from './order.entities';
import { UserCoupon } from './user_coupon.entities';

const { CREATE, UPDATE } = CrudValidationGroups;

@Entity()
export class User {
  @PrimaryGeneratedColumn({type: 'mediumint'})
  id: number;

  @IsOptional()
  @ApiProperty()
  @MaxLength(60, { // here, $constraint1 will be replaced with "50", and $value with actual supplied value
    message: "username is too long. Maximal length is $constraint1 characters, but actual is $value"
  })
  @Column({type: 'varchar' ,length: 60})
  username:string;

  @IsOptional()
  @ApiProperty()
  @MaxLength(60, { // here, $constraint1 will be replaced with "50", and $value with actual supplied value
    message: "nickname is too long. Maximal length is $constraint1 characters, but actual is $value"
  })
  @Column({type: 'varchar' ,length: 60})
  nickname:string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar' ,length: 45})
  register_ip:string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar' ,length: 255})
  avatar:string;

  @IsOptional()
  @ApiProperty()
  @IsMobilePhone('zh-CN')
  @Column({type: 'varchar' ,length: 20})
  mobile:string;

  @IsOptional()
  @ApiProperty()
  //@Exclude()
  @MaxLength(32, { // here, $constraint1 will be replaced with "50", and $value with actual supplied value
    message: "password is too long. Maximal length is $constraint1 characters, but actual is $value"
  })
  @Column({type: 'varchar' ,length: 32})
  password:string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'tinyint' ,width: 1 })
  gender: number;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar' ,length: 50})
  weixin_openid:string;

  @IsOptional()
  @ApiProperty()
  @Column({type:'date'})
  birthday: string;

  @IsOptional()
  @ApiProperty()
  @CreateDateColumn()
  register_time: string;

  @IsOptional()
  @ApiProperty()
  @UpdateDateColumn()
  last_login_time: string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar' ,length: 15})
  last_login_ip: string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'tinyint', width: 3})
  user_level_id: number;

  @IsOptional()
  @ApiProperty()
  @IsEmail()
  @Column({type: 'varchar' ,length: 50})
  email:string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'varchar' ,length: 255})
  email_token:string;

  @IsOptional()
  @ApiProperty()
  @UpdateDateColumn()
  email_token_exptime:string;

  @IsOptional()
  @ApiProperty()
  @Column({type: 'tinyint' ,width: 1,default:0})
  email_status: number

  @OneToOne(type => UserLevel, user_level => user_level.id)
  @JoinColumn({
    name: "user_level_id"
  })
  userLevel: UserLevel

  @OneToMany(type => Address, address => address.user)
  address: Address[]

  @OneToMany(type => Order, order => order.user)
  order: Order[]

  @OneToMany(type => UserCoupon, userCoupon => userCoupon.user)
  userCoupon: UserCoupon[]
}
